package com.zliblike_platform.controller;

import com.zliblike_platform.dto.CommentDTO;
import com.zliblike_platform.entity.User;
import com.zliblike_platform.service.CommentService;
import com.zliblike_platform.vo.CommentVO;
import com.zliblike_platform.vo.PageResult;
import com.zliblike_platform.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 评论控制器
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;
    
    /**
     * 添加书籍评论
     */
    @PostMapping("/api/comment/book/{bookId}")
    @ResponseBody
    public ResponseResult<Long> addBookComment(@PathVariable Long bookId,
                                               @RequestBody CommentDTO commentDTO,
                                               HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "请先登录");
        }
        
        try {
            Long commentId = commentService.addBookComment(loginUser.getId(), bookId, commentDTO);
            return ResponseResult.success(commentId);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 添加书单评论
     */
    @PostMapping("/api/comment/booklist/{listId}")
    @ResponseBody
    public ResponseResult<Long> addListComment(@PathVariable Long listId,
                                               @RequestBody CommentDTO commentDTO,
                                               HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "请先登录");
        }
        
        try {
            Long commentId = commentService.addListComment(loginUser.getId(), listId, commentDTO);
            return ResponseResult.success(commentId);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 删除评论
     */
    @DeleteMapping("/api/comment/{commentId}")
    @ResponseBody
    public ResponseResult<Void> deleteComment(@PathVariable Long commentId,
                                              HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "请先登录");
        }
        
        try {
            boolean success = commentService.deleteComment(loginUser.getId(), commentId);
            if (success) {
                return ResponseResult.success();
            } else {
                return ResponseResult.fail(403, "无权删除该评论");
            }
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 点赞评论
     */
    @PostMapping("/api/comment/{commentId}/like")
    @ResponseBody
    public ResponseResult<Integer> likeComment(@PathVariable Long commentId,
                                               HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "请先登录");
        }
        
        try {
            int likes = commentService.likeComment(loginUser.getId(), commentId);
            return ResponseResult.success(likes);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 取消点赞
     */
    @PostMapping("/api/comment/{commentId}/unlike")
    @ResponseBody
    public ResponseResult<Integer> unlikeComment(@PathVariable Long commentId,
                                                 HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "请先登录");
        }
        
        try {
            int likes = commentService.unlikeComment(loginUser.getId(), commentId);
            return ResponseResult.success(likes);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 获取书籍评论列表
     */
    @GetMapping("/api/book/{bookId}/comments")
    @ResponseBody
    public ResponseResult<PageResult<CommentVO>> getBookComments(@PathVariable Long bookId,
                                                                @RequestParam(defaultValue = "1") int page,
                                                                @RequestParam(defaultValue = "10") int size,
                                                                HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        Long userId = loginUser != null ? loginUser.getId() : null;
        
        try {
            PageResult<CommentVO> comments = commentService.getBookComments(bookId, userId, page, size);
            return ResponseResult.success(comments);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 获取书单评论列表
     */
    @GetMapping("/api/booklist/{listId}/comments")
    @ResponseBody
    public ResponseResult<PageResult<CommentVO>> getListComments(@PathVariable Long listId,
                                                                @RequestParam(defaultValue = "1") int page,
                                                                @RequestParam(defaultValue = "10") int size,
                                                                HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        Long userId = loginUser != null ? loginUser.getId() : null;
        
        try {
            PageResult<CommentVO> comments = commentService.getListComments(listId, userId, page, size);
            return ResponseResult.success(comments);
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
} 